#include "SelectorImpl.h"
#include <assert.h>

ConditionalSelectorImpl::ConditionalSelectorImpl()
	:m_simpleSelector(NULL), m_condition(NULL)
{

}

ConditionalSelectorImpl::ConditionalSelectorImpl(const SimpleSelector& aSimpleSelector,
												 const Condition& aCondition)
{
	m_simpleSelector = dynamic_cast<SimpleSelector*>(aSimpleSelector.Copy());
	m_condition = aCondition.Copy();
}

ConditionalSelectorImpl::ConditionalSelectorImpl(const ConditionalSelectorImpl&
												          aConditionalSelector)
{
	if(aConditionalSelector.m_simpleSelector)
		m_simpleSelector = dynamic_cast<SimpleSelector*>(
								aConditionalSelector.m_simpleSelector->Copy());
	if(aConditionalSelector.m_condition)
		m_condition = aConditionalSelector.m_condition->Copy();
}

ConditionalSelectorImpl::~ConditionalSelectorImpl()
{
	if(m_simpleSelector)
		delete m_simpleSelector;
	if(m_condition)
		delete m_condition;
}

ConditionalSelectorImpl& ConditionalSelectorImpl::operator=(const ConditionalSelectorImpl&
																      aConditionalSelector)
{
	if(this == &aConditionalSelector)
		return *this;

	if(aConditionalSelector.m_simpleSelector)
		setSimpleSelector(*aConditionalSelector.m_simpleSelector);
	if(aConditionalSelector.m_condition)
		setCondition(*aConditionalSelector.m_condition);
	return *this;
}

Selector* ConditionalSelectorImpl::Copy()const
{
	ConditionalSelectorImpl* result = new ConditionalSelectorImpl();
	if(m_simpleSelector)
		result->setSimpleSelector(*m_simpleSelector);
	if(m_condition)
		result->setCondition(*m_condition);
	return result;
}

Selector::SelectorType ConditionalSelectorImpl::getSelectorType()const
{
	return Selector::SAC_CONDITIONAL_SELECTOR;
}

const string* ConditionalSelectorImpl::toString()const
{
	assert(m_simpleSelector && m_condition);

	string* result = NULL;
	try
	{
		result = new string();
		result->append(*(m_simpleSelector->toString()));
		result->append(*(m_condition->toString()));
	}catch(std::exception& e)
	{
		if(result)
			delete result;
		return NULL;
	}
	return result;
}

const SimpleSelector& ConditionalSelectorImpl::getSimpleSelector()const
{
	assert(m_simpleSelector);
	return *m_simpleSelector;
}

const Condition& ConditionalSelectorImpl::getCondition()const
{
	assert(m_condition);
	return *m_condition;
}









